package com.itheima.ydd.controller;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private YddUserService yddUserService;

    /**
     * 用户管理 分页可以带条件查询用户信息
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String sex,String username,String mobile,String page,String limit){
        int pageI = Integer.parseInt(page);
        int pageSize = Integer.parseInt(limit);
        Page<YddUser> pageInfo = new Page<>(pageI, pageSize);
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(sex!=null,YddUser::getSex,sex);
        if (sex!="") {
            queryWrapper.like(YddUser::getSex, sex);
        }
        if (username!="") {
            queryWrapper.like(YddUser::getUsername, username);
        }
        if (mobile!="") {
            queryWrapper.eq(YddUser::getMobile, mobile);
        }
//        queryWrapper.like(username!=null,YddUser::getUsername,username);
//        queryWrapper.eq(mobile!=null,YddUser::getMobile,mobile);
        Page<YddUser> userPage = yddUserService.page(pageInfo, queryWrapper);
        int count = yddUserService.count(queryWrapper);
        List<YddUser> records = userPage.getRecords();
        PageResult pageResult = PageResult.init((long)count, (long) pageSize, (long)pageI, records);
        return ResultData.ok("",pageResult);

    }

    /**
     * 根据id查询单个用户信息 修改对话框的数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        YddUser user = yddUserService.getById(id);
        return ResultData.ok("",user);
    }

    /**
     * 添加单个用户
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddUser yddUser){
        String password = yddUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        yddUser.setPassword(password);

//        birthday = String.valueOf(LocalDateTime.now());
       String birthday = String.valueOf(LocalDate.now());
       yddUser.setBirthday(birthday);
        yddUser.setBirthday(birthday);
        yddUserService.save(yddUser);
        return ResultData.ok("","添加用户成功");
    }

    /**
     * 会员管理 删除单个用户信息
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(String id){
        yddUserService.removeById(id);
        return ResultData.ok("","删除用户成功");
    }

    /**
     * 会员管理 用户管理 修改用户信息
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddUser yddUser){
        if (yddUser==null){
            throw new BusinessException("用户不存在无法修改");
        }
        String password = yddUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        yddUser.setPassword(password);
        yddUserService.updateById(yddUser);
        return ResultData.ok("","修改用户信息成功");
    }
}
